<template>
	<view class="animated fast fadeIn">
		<template v-if="msglist.length > 0">
			<block v-for="(item, index) in msglist" :key="index">
				<msg-list :item="item" :index="index"></msg-list>
			</block>
		</template>
		<template v-else>
			<noThing></noThing>
		</template>

		<view class="">
			<u-popup v-model="show" mode="top">
				<view class="text-center py-2 border-bottom" hover-class="bg-light">
					<text class="iconfont icon-weibiaoti-- mr-2"></text>
					添加好友
				</view>
				<view class="text-center py-2" hover-class="bg-light" @click="clean">
					<text class="iconfont icon-shanchu mr-2"></text>
					清除缓存
				</view>
			</u-popup>
		</view>
	</view>
</template>

<script>
import msgList from '../../compoment/msg/msg-list.vue';
import { mapState } from 'vuex';
/* let demo = [
	{
		avater: '../../static/image/2021051521244130.jpg',
		username: '袁去',
		time: '下午 4:30',
		content: '内容',
		noread: 2
	},
	{
		avater: '../../static/image/2021051521244130.jpg',
		username: '袁去',
		time: '下午 4:30',
		content: '内容',
		noread: 2
	}
]; */
export default {
	data() {
		return {
			msglist: [],
			show: false
		};
	},
	methods: {
		clean() {
			uni.clearStorage({
				success: () => {
					this.msglist = [];
					this.$store.state.SocketTask.close();
					this.$store.state.SocketTask = false;
					this.getstory();
					this.$store.dispatch('updateTabarBadge');
				}
			});
			uni.showToast({
				title: '清理成功',
				icon: 'none'
			});
		},
		getChatList(flush) {
			const userinfo = uni.getStorageSync('userInfo');
			if (userinfo) {
				const list = uni.getStorageSync('chatList_' + userinfo.id);
				this.msglist = list ? JSON.parse(list) : [];
				if (flush) {
					uni.stopPullDownRefresh();
				}
			}
		}
	},
	onLoad() {
		uni.$on('UserChat', () => {
			this.getChatList(false);
		});
	},
	onUnload() {
		uni.$off('UserChat', () => {});
	},
	onShow() {
		this.getChatList(false);
	},
	computed: {
		/* ...mapState({
			msgList: (state) => state.ChatList
		}) */
	},
	filters: {
		fnName(value) {
			return value + 12;
		}
	},
	// 用户点击导航栏的原生图标时触发的事件
	onNavigationBarButtonTap(e) {
		switch (e.index) {
			case 0:
				uni.navigateTo({
					url: '/pages/user-list/user-list'
				});
				break;
			case 1:
				this.show = true;
				break;
		}
	},
	// 监听下拉刷新
	onPullDownRefresh() {
		this.getChatList(true);
	},

	components: {
		msgList
	}
};
</script>

<style></style>
